package com.app.mingluxing.location;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.app.mingluxing.R;
import com.app.mingluxing.base.BaseActivity;
import com.app.mingluxing.timesquare.CalendarPickerView;

import java.util.Calendar;
import java.util.Date;

/**
 * 选择日期 页面
 * Created by Aaron on 15/8/4.
 */
public class ChooseDateActivity extends BaseActivity {

    private Calendar nextYear;
     private  Calendar lastYear;
    protected long defaultTime = 0;// 默认的时间
//    private TextView tv_title;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_choose_date);
        overridePendingTransition(R.anim.translate_in, R.anim.translate_out);

        initWidget();

    }

    private void initWidget() {

        ImageView iv_left = (ImageView) findViewById(R.id.iv_left);
//        iv_left.setImageResource(R.drawable.arrow_down);

        TextView tv_title = (TextView) findViewById(R.id.tv_title);
        tv_title.setText(getResources().getString(R.string.choose_date));

        CalendarPickerView calendar_view = (CalendarPickerView) findViewById(R.id.calendar_view);

        initDefaultDate();

        calendar_view.init(lastYear.getTime(), nextYear.getTime())
                .withSelectedDate(new Date()).inMode(CalendarPickerView.SelectionMode.SINGLE);


        calendar_view.setOnDateSelectedListener(new CalendarPickerView.OnDateSelectedListener() {
            @Override
            public void onDateSelected(Date date) {
//                long time = date.getTime();

                Calendar c = Calendar.getInstance();
                c.setTime(date);

                Date time = c.getTime();


//                time = time + (2 * 60 * 60 * 1000);

//                LogUtil.e("tag", "选择的时间===" + time.getTime() + ",," + time.toLocaleString() + ",," + AbDateUtil.getStringByFormat(time.getTime(), "yyyy-MM-dd HH:mm:ss"));


//                LogUtil.e("TAG", "选择的日期:::===>" + c.get(Calendar.DAY_OF_MONTH));
                Intent data = new Intent();
                data.putExtra("date", time.getTime());

                setResult(24, data);
                finish();

            }

            @Override
            public void onDateUnselected(Date date) {

            }
        });

        iv_left.setOnClickListener(this);


    }

    @Override
    public void widgetClick(View v) {

        switch (v.getId()) {

            case R.id.iv_left:
                finish();

                break;

        }

    }

    private void initDefaultDate() {

        // 日历 初始化
        nextYear = Calendar.getInstance();
        nextYear.add(Calendar.DAY_OF_WEEK, 1);

        lastYear = Calendar.getInstance();
        lastYear.add(Calendar.DAY_OF_WEEK, -6);
        // 需要一个默认值 当天时间向前推两个小时
        Calendar instance = Calendar.getInstance();

        defaultTime = instance.getTime().getTime();


    }


    @Override
    public void finish() {
        super.finish();
        overridePendingTransition(R.anim.translate_in, R.anim.translate_out);
    }
}
